package com.yayo.mangement.facade.impl;

import com.yayo.mangement.converter.SysRoleMenuConverter;
import com.yayo.mangement.facade.SysRoleMenuFacade;
import com.yayo.mangement.model.SysRoleMenuDO;
import com.yayo.mangement.dto.SysRoleMenuDTO;
import com.yayo.mangement.service.SysRoleMenuService;
import com.yayo.mangement.paging.Paging;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yayo.mangement.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author: Yayo
 * @date: 2020-08-07 14:08:42
 */
@Service
public class SysRoleMenuFacadeImpl implements SysRoleMenuFacade{

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleMenuConverter sysRoleMenuConverter;

    @Override
    public Response<Paging<SysRoleMenuDTO>> paging(Integer pageNo, Integer pageSize,Map<String,Object> params){
        Paging<SysRoleMenuDO> sysRoleMenuDOPaing = sysRoleMenuService.paging(pageNo,pageSize,params);
        List<SysRoleMenuDTO> sysRoleMenuDTOList = sysRoleMenuDOPaing.getData().stream().map(sysRoleMenuConverter::DO2DTO).collect(Collectors.toList());
        return Response.ok(new Paging<SysRoleMenuDTO>(sysRoleMenuDOPaing.getTotal(),sysRoleMenuDTOList));
    }

    @Override
    public Response<SysRoleMenuDTO> detail(Long id){
        SysRoleMenuDO sysRoleMenuDO = sysRoleMenuService.findById(id);
        return Response.ok(sysRoleMenuConverter.DO2DTO(sysRoleMenuDO));
    }

    @Override
    public Response create(SysRoleMenuDTO sysRoleMenuDTO){
        SysRoleMenuDO sysRoleMenuDO = sysRoleMenuConverter.DTO2DO(sysRoleMenuDTO);
        Boolean isTrue = sysRoleMenuService.create(sysRoleMenuDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("create.data.error");
    }

    @Override
    public Response update(SysRoleMenuDTO sysRoleMenuDTO){
        SysRoleMenuDO sysRoleMenuDO = sysRoleMenuConverter.DTO2DO(sysRoleMenuDTO);
        Boolean isTrue = sysRoleMenuService.updateById(sysRoleMenuDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("update.date.error");
    }

    @Override
    public Response delete(Long id){
        boolean isTrue = sysRoleMenuService.delete(id);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("delete.date.error");
    }

}